package com.lyc.cart.servlet;

import com.lyc.cart.bean.Cart;
import com.lyc.cart.bean.CartItem;
import com.lyc.cart.service.CartService;
import com.lyc.cart.serviceIpml.CartServiceImpl;
import com.lyc.product.ServiceImpl.ProductServiceImpl;
import com.lyc.product.bean.Product;
import com.lyc.product.service.ProductService;
import com.lyc.user.bean.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/bookInfo")
public class CartServlet extends HttpServlet {
    private ProductService productService = new ProductServiceImpl();
    private CartService cartService = new CartServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        String methodName = req.getParameter("methodName");
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            // 执行方法
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 书籍详情
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getBookByProductId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int productId = 1;
        try {
            productId = Integer.parseInt(req.getParameter("productId"));
        } catch (Exception e) {
        }
        Product bookInfo = productService.getBookByProductId(productId);
        req.setAttribute("bookInfo", bookInfo);
        req.getRequestDispatcher("/jsp/cart.jsp").forward(req, resp);
    }

    //把书籍添加到购物车
    public void addBookToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User userAccount = (User) session.getAttribute("userAccount");
        int id = userAccount.getId();
//        int uid = Integer.parseInt(request.getParameter("uid"));
        int buyNumbers = Integer.parseInt(request.getParameter("buyNumbers"));
        int productId = Integer.parseInt(request.getParameter("productId"));
        Cart cart = new Cart();
        cart.setUid(id);
        cart.setBuyNumber(buyNumbers);
        cart.setProductId(productId);
        String addResult = cartService.addBookToCart(cart);
        response.getWriter().println(addResult);
    }

    /**
     * 查看添加进购物车的商品
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void queryMyCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int uid = Integer.parseInt(request.getParameter("uid"));
        List<CartItem> cartItems = cartService.queryMyCart(uid);
        request.setAttribute("cartItems", cartItems);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/isLogin/myShopCart.jsp");
        requestDispatcher.forward(request, response);
    }

    /**
     * 从购物车列表里删除商品
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void deleteBookToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int productId = Integer.parseInt(request.getParameter("productId"));
        User userAccount = (User) request.getSession().getAttribute("userAccount");
        int id = userAccount.getId();
        CartItem cartItem = new CartItem();
        cartItem.setProductId(productId);
        cartItem.setUid(id);
        String deleteResult = cartService.deleteBookToCart(cartItem);
        response.getWriter().print(deleteResult);
    }

    /**
     * 订单表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void myOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}